import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_taotaoshipin/common/theme/app_colors.dart';
import 'package:flutter_taotaoshipin/common/theme/text_styles.dart';
import 'package:flutter_taotaoshipin/value/sp_key.dart';

extension ThemeModeExtension on ThemeMode {
  String get value => <String>['System', 'Light', 'Dark'][index];
}

class AppTheme {
  static ThemeMode getThemeMode() {
    final String theme = SpUtil.getString(SpKey.theme)!;
    switch (theme) {
      case 'Dark':
        return ThemeMode.dark;
      case 'Light':
        return ThemeMode.light;
      default:
        return ThemeMode.system;
    }
  }

  static ThemeData getTheme({bool isDarkMode = false}) {
    return ThemeData(
      useMaterial3: false,
      fontFamily: 'NowarRounded',
      // errorColor: isDarkMode ? AppColors.dark_red : AppColors.red,
      primaryColor: isDarkMode
          ? AppColors.dark_app_main
          : AppColors.app_main,
      colorScheme: ColorScheme.fromSwatch().copyWith(
        brightness: isDarkMode ? Brightness.dark : Brightness.light,
        secondary: isDarkMode
            ? AppColors.dark_app_main
            : AppColors.app_main,
        primary: isDarkMode
            ? AppColors.dark_app_main
            : AppColors.app_main,
      ),
      // Tab指示器颜色
      indicatorColor: isDarkMode
          ? AppColors.dark_app_main
          : AppColors.app_main,
      // 页面背景色
      scaffoldBackgroundColor: isDarkMode
          ? AppColors.dark_bg_color
          : AppColors.bg_color,
      // 主要用于Material背景色
      canvasColor:
          isDarkMode ? AppColors.dark_material_bg : Colors.white,
      // 文字选择色（输入框选择文字等）
      // textSelectionColor: AppColors.app_main.withAlpha(70),
      // textSelectionHandleColor: AppColors.app_main,
      // 稳定版：1.23 变更(https://flutter.dev/docs/release/breaking-changes/text-selection-theme)
      textSelectionTheme: TextSelectionThemeData(
        selectionColor: AppColors.app_main.withAlpha(70),
        selectionHandleColor: AppColors.app_main,
        cursorColor: AppColors.app_main,
      ),
      textTheme: TextTheme(
        // TextField输入文字颜色
        titleMedium: isDarkMode
            ? TextStyles.textDark
            : TextStyles.text,
        // Text文字样式
        bodyMedium: isDarkMode
            ? TextStyles.textDark
            : TextStyles.text,
        titleSmall: isDarkMode
            ? TextStyles.textDarkGray12
            : TextStyles.textGray12,
      ),
      inputDecorationTheme: InputDecorationTheme(
        hintStyle: isDarkMode
            ? TextStyles.textHint14
            : TextStyles.textDarkGray14,
      ),
      appBarTheme: AppBarTheme(
        elevation: 0.0,
        color: isDarkMode
            ? AppColors.dark_appbar_bg_color
            : AppColors.appbar_bg_color,
        systemOverlayStyle:
            isDarkMode ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark,
        foregroundColor: isDarkMode ? Colors.white70 : Colors.black87,
      ),
      bottomAppBarTheme: BottomAppBarTheme(
        elevation: 0.0,
        color: isDarkMode
            ? AppColors.dark_appbar_bg_color
            : AppColors.appbar_bg_color,
      ),
      dividerTheme: DividerThemeData(
        color: isDarkMode
            ? AppColors.dark_line
            : AppColors.line,
        space: 1,
        thickness: 1,
      ),
      cupertinoOverrideTheme: CupertinoThemeData(
        brightness: isDarkMode ? Brightness.dark : Brightness.light,
        barBackgroundColor: isDarkMode
            ? AppColors.dark_appbar_bg_color
            : AppColors.appbar_bg_color,
        textTheme: CupertinoTextThemeData(
          tabLabelTextStyle: TextStyle(fontFamily: 'NowarRounded'),
          textStyle: TextStyle(fontFamily: 'NowarRounded'),
        ),
      ),
      visualDensity: VisualDensity
          .standard, // https://github.com/flutter/flutter/issues/77142
    );
  }
}
